//封装分类业务数据相关代码
import { getCategoryAPI } from '@/apis/category'
import { onMounted, ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'

export function useCateGory() {
  //调接口组装数据
  const categoryData = ref({})
  const route = useRoute()
  // 如果id没传值就用route.params.id的值
  const getCateGory = async (id = route.params.id) => {
    const res = await getCategoryAPI(id)
    categoryData.value = res.result
  }
  onMounted(() => getCateGory())
  // 解决路由复用缓存，页面切换不更新问题，路由参数变化的时候，将分类数据接口重新发送
  onBeforeRouteUpdate((to) => getCateGory(to.params.id))

  return {
    categoryData
  }
}
